From: tsteven4 Date: Sat, 16 Mar 2019 14:31:16 +0000 (-0600) Subject: replace queues in mkshort. (#327) X-Git-Tag: archive/raspbian/1.10.0+ds-2+rpi1~1^2~12^2~8^2~18 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https://%22Program/%22http:/www.example.com/cgi/%22https:/%22Program?a=commitdiff_plain;h=636654d4bc4a793aa668f78c92130951f18b472b;p=gpsbabel.git replace queues in mkshort. (#327) Also, hide the contents of mkshort_handle_imp. --- diff --git a/defs.h b/defs.h index d2fb72bb2..3e8777179 100644 --- a/defs.h +++ b/defs.h @@ -866,21 +866,8 @@ geocache_container gs_mkcont(const QString& t); * This is an opaque pointer. Callers must not fondle the contents of it. */ // This is a crutch until the new C++ shorthandle goes in. -#define PRIME 37 -typedef struct { - unsigned int target_len; - char* badchars; - char* goodchars; - char* defname; - queue namelist[PRIME]; - - /* Various internal flags at end to allow alignment flexibility. */ - unsigned int mustupper:1; - unsigned int whitespaceok:1; - unsigned int repeating_whitespaceok:1; - unsigned int must_uniq:1; - unsigned int is_utf8:1; -} mkshort_handle_imp; + +struct mkshort_handle_imp; // forward declare, definition in mkshort.cc typedef mkshort_handle_imp* short_handle; char* mkshort(short_handle, const char*); diff --git a/mkshort.cc b/mkshort.cc index 19e62aaf3..c0179e920 100644 --- a/mkshort.cc +++ b/mkshort.cc @@ -19,21 +19,24 @@ */ -#include "defs.h" -#include "cet.h" -#include "cet_util.h" +#include // for isspace, toupper, isdigit +#include // for sprintf, size_t +#include // for strlen, memmove, strchr, strcpy, strncmp, strcat, strncpy + +#include // for QList +#include // for QString +#include // for foreach -#include -#include -#include -#include +#include "defs.h" +#include "cet.h" // for cet_utf8_strdup, cet_utf8_strlen, cet_utf8_strndup +#include "cet_util.h" // for cet_cs_vec_utf8 #define MYNAME "mkshort" static const char vowels[] = "aeiouAEIOU"; -#define DEFAULT_TARGET_LEN 8 +static constexpr unsigned int default_target_len = 8U; static const char* DEFAULT_BADCHARS = "\"$.,'!-"; /* @@ -42,13 +45,27 @@ static const char* DEFAULT_BADCHARS = "\"$.,'!-"; * string hash mixes things up enough that strcmp can generally bail on the * first byte or two for a mismatch. */ -#define PRIME 37 +static constexpr unsigned int prime = 37U; + +struct uniq_shortname { + char* orig_shortname{nullptr}; + int conflictctr{0}; +}; -typedef struct { - queue list; - char* orig_shortname; - int conflictctr; -} uniq_shortname; +struct mkshort_handle_imp { + unsigned int target_len{default_target_len}; + char* badchars{nullptr}; + char* goodchars{nullptr}; + char* defname{nullptr}; + QList namelist[prime]; + + /* Various internal flags */ + bool mustupper{false}; + bool whitespaceok{true}; + bool repeating_whitespaceok{false}; + bool must_uniq{true}; + bool is_utf8{false}; +}; static struct replacements { const char* orig; @@ -76,23 +93,16 @@ unsigned int hash_string(const char* key) while (*key) { hash = ((hash<<5) ^ (hash>>27)) ^ toupper(*key++); } - hash = hash % PRIME; + hash = hash % prime; return hash; } short_handle mkshort_new_handle() { - mkshort_handle_imp* h = (mkshort_handle_imp*) xcalloc(sizeof *h, 1); - - for (auto &i : h->namelist) { - QUEUE_INIT(&i); - } + auto h = new mkshort_handle_imp; - h->whitespaceok = 1; h->badchars = xstrdup(DEFAULT_BADCHARS); - h->target_len = DEFAULT_TARGET_LEN; - h->must_uniq = 1; h->defname = xstrdup("WPT"); h->is_utf8 = (global_opts.charset == &cet_cs_vec_utf8); @@ -103,11 +113,9 @@ static uniq_shortname* is_unique(mkshort_handle_imp* h, char* name) { - queue* e, *t; int hash = hash_string(name); - QUEUE_FOR_EACH(&h->namelist[hash], e, t) { - uniq_shortname* z = reinterpret_cast(e); + foreach (uniq_shortname* z, h->namelist[hash]) { if (0 == case_ignore_strcmp(z->orig_shortname, name)) { return z; } @@ -123,7 +131,7 @@ add_to_hashlist(mkshort_handle_imp* h, char* name) uniq_shortname* s = (uniq_shortname*) xcalloc(1, sizeof(uniq_shortname)); s->orig_shortname = xstrdup(name); - ENQUEUE_TAIL(&h->namelist[hash], &s->list); + h->namelist[hash].append(s); } char* @@ -161,16 +169,14 @@ mkshort_del_handle(short_handle* h) } for (auto &i : hdr->namelist) { - queue* e, *t; - QUEUE_FOR_EACH(&i, e, t) { - uniq_shortname* s = reinterpret_cast(e); + while (!i.isEmpty()) { #if 0 if (global_opts.verbose_status >= 2 && s->conflictctr) { fprintf(stderr, "%d Output name conflicts: '%s'\n", s->conflictctr, s->orig_shortname); } #endif - dequeue(e); + auto s = i.takeFirst(); xfree(s->orig_shortname); xfree(s); } @@ -184,7 +190,7 @@ mkshort_del_handle(short_handle* h) xfree(hdr->defname); } - xfree(hdr); + delete hdr; *h = nullptr; } @@ -252,7 +258,7 @@ setshort_length(short_handle h, int l) { mkshort_handle_imp* hdl = (mkshort_handle_imp*) h; if (l == 0) { - hdl->target_len = DEFAULT_TARGET_LEN; + hdl->target_len = default_target_len; } else { hdl->target_len = l; }